<template>
  <view class="w-full max-w-[750rpx] mx-auto min-h-screen bg-[#f6f8fa] p-[32rpx] box-border">
    <backView />
    <text class="block text-[48rpx] font-extrabold mb-[40rpx] text-center text-blue-600 drop-shadow">被监护人信息详情</text>
    <view class="mb-[48rpx] flex items-center">
      <text class="block text-[32rpx] font-bold">基本信息</text>
      <nut-button class="!ml-auto" plain size="mini" type="success" @click="goToDemographic">人口学信息</nut-button>
      <nut-button class="!ml-12rpx]" plain size="mini" type="warning" @click="goToSocioeconomic">社会经济信息</nut-button>
    </view>
    <FormView v-if="form.phone" :fieldConfig="fieldConfig" :form="form" :showButtons="true" @submit="submitForm" />

    <!-- 操作按钮 -->
    <!-- <view class="mt-[48rpx] flex gap-[24rpx]">
      <nut-button block type="default" class="h-[96rpx] rounded-[16rpx] text-[32rpx] font-bold shadow flex-1" @click="goBack">返回</nut-button>
      <nut-button block type="primary" class="h-[96rpx] rounded-[16rpx] text-[32rpx] font-bold shadow flex-1 bg-blue-500 hover:bg-blue-600 active:bg-blue-700" @click="goEdit">编辑</nut-button>
      <nut-button block type="danger" class="h-[96rpx] rounded-[16rpx] text-[32rpx] font-bold shadow flex-1" @click="deleteRecord">删除</nut-button>
    </view> -->
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import Taro from "@tarojs/taro";
import { guardianInfoFields as fieldConfig } from "./fields";
import FormView from "@/components/FormView.vue";
import { request } from "@/service/request";
import { localStg } from "@/utils";
import { getAgeByIdCard, getGenderByIdCard } from "@/utils/idTooler";
import backView from "@/components/backView.vue";

const form = ref<any>({});
const recordId = ref<string>("");

onMounted(async () => {
  // 获取路由参数
  const params = Taro.getCurrentInstance().router?.params;
  console.log(params, "params---------=======================");
  // if (params?.idcard) {
  //   // recordId.value = params.id;
  //   await loadRecordData(params.idcard);
  // }
  if(params?.type){
    form.value = localStg.get("guardianedInfo");
    console.log(form.value, "form.value---------=======================");
  }
});

async function loadRecordData(id: string) {
  const result = await request.get<any>(
    "/nurse_user/nurseUser/list",
    {
      idcard: id,
    },
    { useErrMsg: true }
  );
  console.log(result, "result---------=======================");
  form.value = result.success.records[0];
  form.value.sex = getGenderByIdCard(id);
  form.value.age = getAgeByIdCard(id);

  // const rs = await request.get<any>(
  //   "/nurse_user/nurseUser/listNurseUserFamilyByMainId",
  //   {
  //     userId: form.value.id,
  //   },
  //   { useErrMsg: true }
  // );
  // console.log(rs, "rs---------=======================");
}

async function submitForm(form: any, fieldConfig: any) {
  try {
    // 提交数据
    const params = {
      ...form,
    }
    
    const result = await request.put<any>(`/nurse_user/nurseUser/editNurseUserFamily`, params, {
      useErrMsg: true
    })
    
    if (result) {
      Taro.showToast({ title: '更新成功', icon: 'success' })
      // 返回上一页
      setTimeout(() => {
        Taro.navigateBack()
      }, 1500)
    }
  } catch (error) {
    console.error('更新失败:', error)
    Taro.showToast({ title: '更新失败', icon: 'error' })
  }
}

function goBack() {
  Taro.navigateBack();
}

function goEdit() {
  if (recordId.value) {
    Taro.navigateTo({
      url: `/pages/info/guardian/edit?id=${recordId.value}`,
    });
  }
}

function goToDemographic() {
  if (form.value.idcard) {
    Taro.navigateTo({
      url: `/pages/info/demographic/edit?idcard=${form.value.idcard}`,
    });
  }
}

function goToSocioeconomic() {
  if (form.value.idcard) {
    Taro.navigateTo({
      url: `/pages/info/socioeconomic/edit?idcard=${form.value.idcard}`,
    });
  }
}

async function deleteRecord() {
  try {
    const { confirm } = await Taro.showModal({
      title: "确认删除",
      content: "确定要删除这条监护人信息吗？",
      confirmText: "删除",
      cancelText: "取消",
    });

    if (confirm && recordId.value) {
      const result = await request.delete<any>(
        `/guardian/guardianInfo/delete/${recordId.value}`,
        {},
        {
          useErrMsg: true,
        }
      );

      if (result) {
        Taro.showToast({ title: "删除成功", icon: "success" });
        setTimeout(() => {
          Taro.navigateBack();
        }, 1500);
      }
    }
  } catch (error) {
    console.error("删除失败:", error);
    Taro.showToast({ title: "删除失败", icon: "error" });
  }
}
</script>
